<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<meta charset="utf-8" />
	<title></title>
	<link href="../css/bootstrap.min.css" rel="stylesheet">
	<link href="../css/bootstrap-select.css" rel="stylesheet">
    <link rel="stylesheet" href="../css/bootstrap-datetimepicker.min.css">
    <link rel="stylesheet" href="../css/bootstrap-treeview.min.css">
	<link href="../css/font-awesome.min.css" rel="stylesheet">
	<link href="../css/JyyCommon.css" rel="stylesheet">
	<link href="../css/JyyTable.css" rel="stylesheet">
	<link href="../css/JyyTree.css" rel="stylesheet">
	<link href="../css/JyyMsgBox.css" rel="stylesheet">
	<script type="text/javascript" src="../js/jquery-3.2.1.js"></script>
	<script type="text/javascript" src="../js/jquery.form.min.js"></script> 
	<script type="text/javascript" src="../js/jquery-ui.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-datetimepicker.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-paginator.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-treeview.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-select.js"></script>
	<script type="text/javascript" src="../js/jyy_modal.js"></script>
	<script type="text/javascript" src="../js/JyyCommon.js"></script>
	<script type="text/javascript" src="../js/JyyTable.js"></script>
	<script type="text/javascript" src="../js/JyyTree.js"></script>
	<script type="text/javascript" src="../js/JyyMsgBox.js"></script>
	<script type="text/javascript" src="../js/JyyLoading.js"></script>
	<script type="text/javascript" src="../js/echarts.js"></script>
	<script type="text/javascript" src="../../utils/CommonUtil.js"></script>
	<style>
	.heidenbt{
		display: none
	}
</style>
	<script type="text/javascript">
		var tree = null;
		var projectId =  getQueryString("projectId")||'88888888888';
		$(function(){
			getDatePicker();
		});
		function selectData(){
			$("#operate").css("display","block");
			var dateSel =  $('#dateSel').val(); 
			if(JyyCommon.isEmpty(dateSel)){
				JyyMsgBox.alert({ title:"警告", message:"[日期]不可为空",type:"warning"}).on(function (e) {});
				return false;
			}
			JyyLoading.start();
			$.when(getEcahtData(dateSel)).done(function(echatData){
				createEchat(echatData.respData, 1);
				createEchat(echatData.respData, 2);
				createEchat(echatData.respData, 3);
				//createEchat(echatData, 4);
				
				JyyLoading.end();
			});
		}
		function createEchat(echartData, code) {
			var id = "";
			var option = {};
			var xAxisName = "";
			var csvName = "echartData";
			if(code == 1) {
				id = "yearEchart";
				xAxisName = "月份";
				csvName = "年负荷特性曲线";
			} else if(code == 2) {
				id = "monthEchart";
				xAxisName = "日";
				csvName = "月负荷特性曲线";
			} else if(code == 3) {
				id = "dayEchart";
				xAxisName = "时刻";
				csvName = "日负荷特性曲线";
			} else if(code == 4) {
				id = "hourEchart";
				xAxisName = "时间";
			}
			var xAxisData = echartData[id].xAxisData;
			var seriesData = echartData[id].seriesData;
			option = {
				tooltip: {
			    	trigger: 'axis',
			    	axisPointer: {
			      		type: 'cross',
			      		label: {
			        		backgroundColor: '#6a7985'
			      		}
			    	},
			    	position: function(point, params, dom, rect, size) {
			        	//其中point为当前鼠标的位置，size中有两个属性：viewSize和contentSize，分别为外层div和tooltip提示框的大小
			            var x = point[0]; //
			            var y = point[1];
			            var viewWidth = size.viewSize[0];
			            var viewHeight = size.viewSize[1];
			            var boxWidth = size.contentSize[0];
			            var boxHeight = size.contentSize[1];
			            var posX = 0; //x坐标位置
			            var posY = 0; //y坐标位置

			            if (x < boxWidth) { //左边放不开
			                posX = 5;
			            } else { //左边放的下
			                 posX = x - boxWidth;
			            }

			            if (y < boxHeight) { //上边放不开
			                 posY = 5;
			            } else { //上边放得下
			                 posY = y - boxHeight;
			            }

			            return [posX, posY];
					}
				},
				toolbox: {
					show: true,
					right: 30,
				    itemSize: 20,
					itemGap: 12,
			        feature: {
			        	myTool: {
			        		show: true,
			        		title: "导出为Excel",
			        		icon: 'image://../images/excel.png',
			        		onclick: (o) => {
								var option = o.option;
								var dataSource = [];
								var dataType = "\uFEFF"; //解决乱码问题
								dataType += option.xAxis[0].name+",";  //添加表格的头
								if(option.series.length > 0) {
									for(var i = 0;i < option.series.length;i++) {
										dataType += option.series[i].name+","
									}
									dataType = dataType.substr(0, dataType.length-1)
								}
								dataType += '\n'; // 以上是导出的Excel文件头部

								for(var j = 0;j < option.legend.length+1;j++) {
									for(var i = 0;i < option.xAxis[0].data.length;i++) {
										dataSource.push([option.xAxis[0].data[i], option.series[0].data[i]]);
									}
								}
								// 从dataSource中取出数据存入数据源（dataType）
								dataSource.map(item => dataType += `${item.join(',')}\n`); 
								// 使用Blob，获得二进制实例
								const csvData = new Blob([dataType], {
								    type: 'text/csv'
								});
								// 创建a标签
								const _a = document.createElement('a');
								_a.href = URL.createObjectURL(csvData);
								_a.target = '_blank';
								_a.download = csvName +`.csv`;
								document.body.appendChild(_a);
								_a.click();
								document.body.removeChild(_a);
		                   	},
			        	},
			        	saveAsImage: {name:csvName,icon:"image://../images/downpic.png"}
			        }
			    },
			  	grid: {
			    	left: '3%',
			    	right: '8%',
			    	bottom: '3%',
			    	containLabel: true
			  	},
			  	xAxis: [{
			    	type: 'category',
			    	name: xAxisName,
			      	boundaryGap: false,
			      	data: xAxisData
			    }],
			  	yAxis: [{
			      	type: 'value',
			      	name: "MW"
			    }],
			  	series: [{
			  		data: seriesData,
			  		type: "line",
			  		name: "值"
			  	}]
			};
			if(code == 4) {
				option.dataZoom = {
			    	start: 0,
			        type: "inside"
			    };
			}
			var chartDom = document.getElementById(id);
			var myChart = echarts.init(chartDom);
			myChart.clear();
			myChart.setOption(option);
			window.addEventListener("resize", () => {
				if(myChart){
					myChart.resize()
				}
			})
		}
		
		//查询
		function select(){
			selectData();
		}
		
		function getQueryString (name){
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			return r ? unescape(r[2]) : null;
		}

		function getDatePicker(){
			$.ajax({
				url: "Sample/selectloadCharactDate",
				type:'POST',
				cache:false,
				dataType: "json",
				data:JSON.stringify({ projectId : projectId }),
				contentType:"application/json",
	            success:function(data){
	            	if(!data.respData.readOnly){
		    			$("#dateSel").datetimepicker({
		    				format : 'yyyy-mm-dd', // 展现格式
		    				autoclose : true, // 选择日期后关闭
		    				language : 'zh-CN', //显示语言为中文
		    				minView : "2",
		    				startDate : new Date(data.respData.startDate),
		    				endDate : new Date(data.respData.endDate)
		    			});
		    			$("#dateSel").datetimepicker("setDate", new Date(data.respData.startDate));
		    			selectData();
	            	} else {
		    			$("#dateSel").datetimepicker({
		    				format : 'yyyy-mm-dd', // 展现格式
		    				autoclose : true, // 选择日期后关闭
		    				language : 'zh-CN', //显示语言为中文
		    				minView : "2",
		    				startDate : new Date(),
		    				endDate : new Date()-1000*60*60*24
		    			});
						JyyMsgBox.alert({ title:"警告", message:"暂无相关数据",type:"warning"}).on(function (e) {});
	            	}
	            }
			});
		}
		
		function getEcahtData(date){
			var res = $.ajax({
				url: "Sample/selectloadCharactEchatData",
				type:'POST',
				cache:false,
				dataType: "json",
				data:JSON.stringify({ projectId : projectId, date : date }),
				contentType:"application/json",
	            success:function(data){
					return data;
	            }
			});
			return res;
		}
		function invoke(url, args, callback){
			var r, op = {
					url: '../g/service/Sample/' + url,
					type:'POST',
					cache:false,
					async:true,
					dataType:'json',
					data:args ? JSON.stringify(args) : null,
					contentType:"application/json"
				};
			if(typeof callback == 'function'){
				op.success = function(data){
					if(data.error)return alert(data.error);
					callback(data);
				}
			} else {
				op.async = false;
				op.success = function(data){
					r = data
				};
			}
			$.ajax(op);
			if(r)if(r.error){
				alert(r.error);
				return null;
			}
			return r;
		};
	</script>
<style type="text/css">
	.selBtn {
		float:left;
		margin-left:20px;
		height:30px;
		width:70px;
		background-color:#1890FF;
		border-radius:5px;
		border: 0px;
		color:white;
	}
	.selBtn:hover {
		background-color: #00BFFF;
	}
	.shadow {
		box-shadow: 0 0.05rem 1rem rgba(0,0,0,0.15)!important;
		border-radius: 0.25rem!important;
		margin-left: 20px;
		margin-bottom: 20px;
	}
</style>
</head>
<body style="position:absolute;top:0px;left:0px;right:0px;bottom:0px;">
	<div id="operate" style="position: absolute;top: 0px;left: 20px;right: 0px;height: 50px;">
		<div class="col-md-12" style="position: absolute;top: 10px;padding-left: 5px;padding-right: 5px;">

			<label  class="control-label col-sm-1" style="float: left;margin-top: 7px;padding: 0px;width: 60px;font-weight: normal;">日期</label>
            <input id="dateSel" class="form-control" placeholder="请选择日期" readonly style="float: left;margin-top: -3px;padding: 5px;width: 184px;font-weight: normal;">

			<button class="selBtn" onclick=select()>查询</button>
		</div>
	</div>
	<div style="width: 100%;overflow-y: scroll;height: calc(100% - 50px);position: absolute;top: 50px;">
		<div class="shadow" style="position: relative;width: calc(100% - 40px);min-height: 450px;">
			<p style="margin-top: 10px;padding-top: 10px;">
				<span style="color: #1890FF;font-weight: 700;font-size: 16px;margin-left: 20px;font-family: '黑体 Bold', '黑体';">年负荷特性曲线</span>
			</p>
			<div id="yearEchart" style="width: 100%;height: 400px;"></div>
		</div>
		<div class="shadow" style="position: relative;width: calc(100% - 40px);min-height: 450px;">
			<p style="margin-top: 10px;padding-top: 10px;">
				<span style="color: #1890FF;font-weight: 700;font-size: 16px;margin-left: 20px;font-family: '黑体 Bold', '黑体';">月负荷特性曲线</span>
			</p>
			<div id="monthEchart" style="width: 100%;height: 400px;"></div>
		</div>
		<div class="shadow" style="position: relative;width: calc(100% - 40px);min-height: 450px;">
			<p style="margin-top: 10px;padding-top: 10px;">
				<span style="color: #1890FF;font-weight: 700;font-size: 16px;margin-left: 20px;font-family: '黑体 Bold', '黑体';">日负荷特性曲线</span>
			</p>
			<div id="dayEchart" style="width: 100%;height: 400px;"></div>
		</div>
		<!-- <div class="shadow" style="position: relative;width: calc(100% - 40px);min-height: 450px;">
			<p style="margin-top: 10px;padding-top: 10px;">
				<span style="color: #1890FF;font-weight: 700;font-size: 16px;margin-left: 20px;font-family: '黑体 Bold', '黑体';">8760小时负荷曲线</span>
			</p>
			<div id="hourEchart" style="width: 100%;height: 400px;"></div>
		</div> -->
	</div>
</body>
</html>